//
//  HOTUserInfo.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-19.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTUserInfo.h"
#import "HOTUtils.h"
#import "HOTHttpClient.h"
#import "APService.h"

@interface HOTUserInfo () {
    
    BOOL _isLogined;
}
@property(nonatomic,strong)NSMutableArray *jobTypeList;
@property(nonatomic,strong)NSMutableArray *cityList;
@property(nonatomic,strong)NSMutableArray *certificationList;
@property(nonatomic,strong)NSMutableArray *healthList;
@end

@implementation HOTUserInfo

+(instancetype)defaultUser {
    
    static dispatch_once_t onceToken;
    static HOTUserInfo *instance;
    dispatch_once(&onceToken, ^{
        
        instance = [[[self class] alloc] init];
        
        [instance restoreFromUserDef];
    });
    
    return instance;
}

/**
 *  是否已经登陆
 *
 *  @return
 */
-(BOOL)isLogined {
    
    return _isLogined;
}

/**
 *  是否自动登陆
 *
 *  @return
 */
-(BOOL)isAutoLogin {
    
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    BOOL autologinFlg = [userDef boolForKey:@"KEY_AUTO_LOGIN_FLG"];
    
    return autologinFlg;
}


/**
 *  登陆时调用 保存用户基本信息
 *
 *  @param dic 
 */
-(void)loginWithDic:(NSDictionary *)dic withAutologin:(BOOL)autologin{
    
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_ID]] forKey:REQ_KEY_USER_ID];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_TOKEN]]  forKey:REQ_KEY_USER_TOKEN];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_NICK]] forKey:REQ_KEY_USER_NICK] ;
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_EMAIL]]  forKey:REQ_KEY_USER_EMAIL];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_PWD]]  forKey:REQ_KEY_USER_PWD];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_SEX]]  forKey:REQ_KEY_USER_SEX];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_BIRTH]]  forKey:REQ_KEY_USER_BIRTH];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_USER_CITY_ID]]  forKey:REQ_KEY_USER_CITY_ID];
    [userDef setObject:[HOTUtils getString:[dic objectForKey:REQ_KEY_DISTRICT]]  forKey:REQ_KEY_DISTRICT];
    
    [userDef setBool:autologin forKey:@"KEY_AUTO_LOGIN_FLG"];
    
    [userDef synchronize];
    
    [self restoreFromUserDef];
    
}

-(void)resgistWithId:(NSString *)uId token:(NSString *)token {
    
    //清空当前数据
    [self clearData];
    self.userId = uId;
    self.token = token;
    
    [[NSUserDefaults standardUserDefaults] setObject:self.userId forKey:REQ_KEY_USER_ID];
    [[NSUserDefaults standardUserDefaults] setObject:self.token forKey:REQ_KEY_USER_TOKEN];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self setValue:@YES forKey:@"_isLogined"];
    
    [self uploadUserToken];
}

-(void)restoreFromUserDef {
    
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    NSString *uid = [userDef objectForKey:REQ_KEY_USER_ID];
    NSString *token = [userDef objectForKey:REQ_KEY_USER_TOKEN];
    
    [self clearData];
    if ([HOTUtils isNotNullOrEmpty:uid] && [HOTUtils isNotNullOrEmpty:token]) {
        
        self.userId = uid;
        self.token = token;
        
        self.nick = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_NICK]];
        self.email = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_EMAIL]];
        self.pwd = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_PWD]];
        self.sex = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_SEX]];
        self.birth = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_BIRTH]];
        self.city_id = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_CITY_ID]];
        self.district = [HOTUtils getString:[userDef objectForKey:REQ_KEY_DISTRICT]];
        
        self.blood = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_BLOOD]];
        self.health = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_HEALTH]];
        self.self_intro = [HOTUtils getString:[userDef objectForKey:REQ_KEY_SELF_INTRO]];
        self.job_id = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_JOB_ID]];
        self.job_name = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_JOB_NAME]];
        self.certification = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USEr_CERTIFICATION_ID]];
        self.rate = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_RATE]];
        self.follow_sum = [HOTUtils getString:[userDef objectForKey:REQ_KEY_USER_FOLLOW_SUM]];
        self.push_token = [APService registrationID];
        
        [self setValue:@YES forKey:@"_isLogined"];
    } else {
        [self setValue:@NO forKey:@"_isLogined"];
    }
}

-(void)save {
    
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    
    [userDef setObject:[HOTUtils getString:self.userId] forKey:REQ_KEY_USER_ID];
    [userDef setObject:[HOTUtils getString:self.token]  forKey:REQ_KEY_USER_TOKEN];
    [userDef setObject:[HOTUtils getString:self.nick] forKey:REQ_KEY_USER_NICK] ;
    [userDef setObject:[HOTUtils getString:self.email]  forKey:REQ_KEY_USER_EMAIL];
    [userDef setObject:[HOTUtils getString:self.pwd]  forKey:REQ_KEY_USER_PWD];
    [userDef setObject:[HOTUtils getString:self.sex]  forKey:REQ_KEY_USER_SEX];
    [userDef setObject:[HOTUtils getString:self.birth]  forKey:REQ_KEY_USER_BIRTH];
    [userDef setObject:[HOTUtils getString:self.city_id]  forKey:REQ_KEY_USER_CITY_ID];
    
    [userDef setObject:[HOTUtils getString:self.blood] forKey:REQ_KEY_USER_BLOOD];
    [userDef setObject:[HOTUtils getString:self.health]  forKey:REQ_KEY_USER_HEALTH];
    [userDef setObject:[HOTUtils getString:self.district] forKey:REQ_KEY_DISTRICT] ;
    [userDef setObject:[HOTUtils getString:self.self_intro]  forKey:REQ_KEY_SELF_INTRO];
    [userDef setObject:[HOTUtils getString:self.job_id]  forKey:REQ_KEY_USER_JOB_ID];
    [userDef setObject:[HOTUtils getString:self.job_name]  forKey:REQ_KEY_USER_JOB_NAME];
    [userDef setObject:[HOTUtils getString:self.certification]  forKey:REQ_KEY_USEr_CERTIFICATION_ID];
    [userDef setObject:[HOTUtils getString:self.rate]  forKey:REQ_KEY_USER_RATE];
    [userDef setObject:[HOTUtils getString:self.follow_sum]  forKey:REQ_KEY_USER_FOLLOW_SUM];
    [userDef setObject:[HOTUtils getString:self.push_token]  forKey:REQ_KEY_USER_PUSH_TOKEN];

    
    [userDef synchronize];
}

-(void)clearData {
    
    self.userId = @"";
    self.token = @"";
    self.nick = @"";
    self.email = @"";
    self.pwd = @"";
    self.sex = @"";
    self.birth = @"";
    self.city_id = @"";
    self.menuList = nil;
    self.blood = @"";
    self.health = @"";
    self.district = @"";
    self.self_intro = @"";
    self.job_id = @"";
    self.certification = @"";
    self.rate = @"";
    self.follow_sum = @"";
    self.push_token = @"";
    
}

-(NSString *)bloodName {
    
    
    return [self bloodNameWithId:self.blood];
}

-(NSString *)bloodNameWithId:(NSString *)bloodId {
    
    if ([bloodId isEqualToString:@"1"]) {
        return @"A型";
    } else if ([bloodId isEqualToString:@"2"]) {
        return @"B型";
    } else if ([bloodId isEqualToString:@"3"]) {
        return @"O型";
    } else if ([bloodId isEqualToString:@"4"]) {
        return @"AB型";
    }
    
    return @"";
}

-(NSString *)jobNameWithId:(NSString *)jobId jobName:(NSString *)jobName{
    
    if (![HOTUtils isNullOrEmpty:jobName]) {
        
        return jobName;
    }
    
    if (self.jobTypeList == nil) {
        
        self.jobTypeList = [[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"jobType" ofType:@"plist"]] init];
    }
    
    for (int i =0; i < self.jobTypeList.count; i ++) {
        
        NSDictionary *dic = [self.jobTypeList objectAtIndex:i];
        if ([[dic objectForKey:@"jid"] isEqualToString:jobId]) {
            
            return [HOTUtils getString:[dic objectForKey:@"jname"]];
        }

    }
    
    return @"";
}

-(NSString *)jobName {
    
    return [self jobNameWithId:_job_id jobName:_job_name];
}

-(NSString *)cityNameWithId:(NSString *)cityid {
    
    if ([HOTUtils isNullOrEmpty:cityid]) {
        
        return @"";
    }
    
    if (self.cityList == nil) {
        
        self.cityList = [[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"zenkoku" ofType:@"plist"]] init];
    }

    for (int i =0; i < self.cityList.count; i ++) {
        
        NSDictionary *dic = [self.cityList objectAtIndex:i];
        if ([[dic objectForKey:@"id"] isEqualToString:cityid]) {
            
            return [HOTUtils getString:[dic objectForKey:@"name"]];
        }
        
    }
    
    return @"";
}

-(NSString *)cityName {
    
    return [self cityNameWithId:self.city_id];
}

-(NSString *)certificationNameWithId:(NSString *)cid {
    
    if ([HOTUtils isNullOrEmpty:cid]) {
        
        return @"";
    }
    
    if (self.certificationList == nil) {
        
        self.certificationList = [[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"certification" ofType:@"plist"]] init];
    }

    for (int i =0; i < self.certificationList.count; i ++) {
        
        NSDictionary *dic = [self.certificationList objectAtIndex:i];
        if ([[dic objectForKey:@"cid"] isEqualToString:cid]) {
            
            return [HOTUtils getString:[dic objectForKey:@"cname"]];
        }
        
    }
    
    return @"";
}

-(NSString *)certificationName {
    
    return [self certificationNameWithId:self.certification];
}

-(NSString *)healthNameWithId:(NSString *)hid {
    
    if ([HOTUtils isNullOrEmpty:hid]) {
        
        return @"";
    }
    
    if (self.healthList == nil) {
        
        self.healthList = [[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"health" ofType:@"plist"]] init];
    }
    
    for (int i =0; i < self.healthList.count; i ++) {
        
        NSDictionary *dic = [self.healthList objectAtIndex:i];
        if ([[dic objectForKey:@"hid"] isEqualToString:hid]) {
            
            return [HOTUtils getString:[dic objectForKey:@"hname"]];
        }
        
    }
    
    return @"";
}

-(NSString *)healthName {
    
    return [self healthNameWithId:self.health];
}

-(NSString *)follow_sum {
    
    return [HOTUtils getString:_follow_sum defaultStr:@"0"];
}

-(NSString *)rate {
    
    return [HOTUtils getString:_rate defaultStr:@"0"];
}

-(NSString *)sexName {
    
    return [self sexNameWithId:self.sex];
}


-(NSString *)sexNameWithId:(NSString *)sid {
    
    if ([sid isEqualToString:@"2"]) {
        
        return @"女";
    } if ([sid isEqualToString:@"1"])  {
        
        return @"男";
    }
    
    return @"";
}

-(void)setPush_token:(NSString *)push_token {
    
    _push_token = push_token;
    [self uploadUserToken];
}

-(void)uploadUserToken {
    
    if ([HOTUtils isNotNullOrEmpty:self.push_token]
        && [HOTUtils isNotNullOrEmpty:_userId]
        && [HOTUtils isNotNullOrEmpty:_token]) {
        
        HOTHttpClient *httpClient = [HOTHttpClient client];
        
        NSDictionary *parameters = @{REQ_KEY_USER_ID:self.userId,
                                     REQ_KEY_USER_TOKEN:self.token,
                                     @"push_token":self.push_token,
                                     REQ_KEY_CLIENT_TYPE:kClientType};
        
        [httpClient requestWithURL:HOT_URL_SAVE_JPUSH Parameters:parameters success:^(id responseObject) {
            
            NSLog(@"====服务器返回数据：%@", responseObject);
            NSString *result = [responseObject objectForKey:REQ_KEY_RESULT];
            NSString *errMsg = [responseObject objectForKey:REQ_KEY_MSG];
            if ([result isEqualToString:@"1"]) {
                //接口调用成功，但result不等于1的场合
                NSLog(@"用户token 上传成功");
                
            } else {
                //接口调用成功
                NSLog(@"用户token 上传失败 error = %@", errMsg);
            }
            
        } failure:^(NSError *error) {
            
            NSLog(@"用户token 上传失败 error = %@", error);
        }];
    }
}

@end
